package com.aoxon.cargo.loader;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aoxon.cargo.activity.PurSupplierDetailsActivity;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.service.SupplierInformationService;
import com.aoxon.cargo.state.DataState;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.ExecutorUtil;

/**
 * 供应商详细信息的加载
 * @author ZZJ
 *
 */
public class SupInfoLoader {

	private static final String TAG = "SupInfoLoader";

	public static Map<String, Supplier> suppliers = new HashMap<String, Supplier>();
	private Map<RelativeLayout, String> layouts = new HashMap<RelativeLayout, String>();

	private SupplierInformationService supplierInformationService = (SupplierInformationService) MyServiceFactory
			.getInstance(MyServiceFactory.SUPPLIER_INFORMATION);

	private Activity activity;

	public SupInfoLoader(Activity activity) {
		this.activity = activity;
		suppliers.clear();
	}

	/**
	 * 清空缓存
	 * @param clearSupplirs 是否清除供应商缓存信息
	 */
	public void clear(boolean clearSupplirs){
		if(clearSupplirs){
			suppliers.clear();
		}
		layouts.clear();
	}
	
	/**
	 * 显示信息
	 * @param supId
	 * @param tvCompany
	 * @param tvAddress
	 * @param ivSupCertification
	 * @param rlSupInformation
	 */
	public void displayInfo(int supId, TextView tvCompany, TextView tvAddress,
			ImageView ivSupCertification, RelativeLayout rlSupInformation) {

		SupInfoToLoad supInfoToLoad = new SupInfoToLoad(supId, tvCompany,
				tvAddress, ivSupCertification, rlSupInformation);

		rlSupInformation.setTag(String.valueOf(supId));
		layouts.put(rlSupInformation, String.valueOf(supId));

		if (suppliers.containsKey(supId)) {
			//先从缓存中查看有无对应数据
			setInfo(suppliers.get(supId), supInfoToLoad);
		} else {

			//缓存没有的话则从网上加载
			Log.i(TAG, "SupInfoLoader to load supId = " + supId);

			ExecutorUtil.getCachedExecutorService().submit(
					new InfoLoader(supInfoToLoad));
		}

	}

	/**
	 * 显示供应商信息
	 * @param supplier
	 * @param supInfoToLoad
	 */
	private void setInfo(Supplier supplier, final SupInfoToLoad supInfoToLoad) {
		supInfoToLoad.tvCompany.setText(supplier.getStoreName());
		supInfoToLoad.tvAddress.setText("地址:" + supplier.getSupCity()
				+ supplier.getsupMarket() + supplier.getsupStall());

		if (supplier.getCerState() == DataState.SUCCESS) {
			supInfoToLoad.ivSupCertification.setVisibility(View.VISIBLE);
		} else {
			supInfoToLoad.ivSupCertification.setVisibility(View.GONE);
		}

		supInfoToLoad.rlSupInfo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String tag = supInfoToLoad.rlSupInfo.getTag().toString();
				Supplier supplier = suppliers.get(tag);
				Intent intent = new Intent(activity.getBaseContext(),
						PurSupplierDetailsActivity.class);
				intent.putExtra("strSupplier", DataUtil.gson.toJson(supplier));
				activity.startActivityForResult(intent, 1);
			}
		});
	}

	/**
	 * 判断是否错位
	 * @param supInfoToLoad
	 * @return
	 */
	private boolean viewReused(SupInfoToLoad supInfoToLoad) {
		String tag = layouts.get(supInfoToLoad.rlSupInfo);
		if (tag == null || !tag.equals(String.valueOf(supInfoToLoad.supId)))
			return true;
		return false;
	}

	/**
	 * 加载供应商信息的线程操作
	 * @author ZZJ
	 *
	 */
	class InfoLoader implements Runnable {

		private SupInfoToLoad supInfoToLoad;

		public InfoLoader(SupInfoToLoad supInfoToLoad) {
			this.supInfoToLoad = supInfoToLoad;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			if (!viewReused(supInfoToLoad)) {
				GsonBean gsonBean = supplierInformationService.execute(supInfoToLoad.supId);
				try {
					Supplier supplier = DataUtil.gson.fromJson(gsonBean.getStrJson(),
							Supplier.class);
					suppliers
							.put(String.valueOf(supplier.getSupId()), supplier);

					if (viewReused(supInfoToLoad))
						return;

					InfoDisplay infoDisplay = new InfoDisplay(supplier,
							supInfoToLoad);

					Activity activity = (Activity) supInfoToLoad.rlSupInfo
							.getContext();
					activity.runOnUiThread(infoDisplay);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * 加载完成后显示信息的线程操作
	 * @author ZZJ
	 *
	 */
	class InfoDisplay implements Runnable {

		private Supplier supplier;
		private SupInfoToLoad supInfoToLoad;

		public InfoDisplay(Supplier supplier, SupInfoToLoad supInfoToLoad) {
			this.supplier = supplier;
			this.supInfoToLoad = supInfoToLoad;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			if (viewReused(supInfoToLoad))
				return;
			if (supplier != null) {
				setInfo(supplier, supInfoToLoad);
			}
		}

	}

	
	/**
	 * 用于缓存要显示的对应的控件
	 * @author ZZJ
	 *
	 */
	class SupInfoToLoad {
		public int supId;
		public TextView tvCompany;
		public TextView tvAddress;
		public RelativeLayout rlSupInfo;
		public ImageView ivSupCertification;

		public SupInfoToLoad(int supId, TextView tvCompany, TextView tvAddress,
				ImageView ivSupCertification, RelativeLayout rlSupInfo) {
			this.supId = supId;
			this.tvAddress = tvAddress;
			this.tvCompany = tvCompany;
			this.rlSupInfo = rlSupInfo;
			this.ivSupCertification = ivSupCertification;
		}

	}

}
